package com.bitcola.security.activity.controller;

import com.alibaba.fastjson.JSONObject;
import com.bitcola.security.activity.biz.BalanceChangeBiz;
import com.bitcola.security.activity.biz.MiningBiz;
import com.bitcola.security.activity.biz.UserInviterBiz;
import com.bitcola.security.activity.entity.UserNet;
import com.bitcola.security.auth.client.annotation.IgnoreUserToken;
import com.bitcola.security.common.context.BaseContextHandler;
import com.bitcola.security.common.msg.AppResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("mining")
public class MiningController {

    @Autowired
    BalanceChangeBiz balanceChangeBiz;
    @Autowired
    MiningBiz miningBiz;



    @Autowired
    UserInviterBiz userInviterBiz;
    @RequestMapping(value = "queryStage")
    @IgnoreUserToken
    public AppResponse queryStage()
    {
        return AppResponse.ok().data(miningBiz.getPower());
    }


    @RequestMapping(value = "getAllPoints")
    @IgnoreUserToken
    public AppResponse queryAllPoints(String userId)
    {
        return AppResponse.ok().data(miningBiz.getAllPoint(userId));

    }

    @RequestMapping(value = "buyPoint")
    public AppResponse buyPoint(String pointId)
    {
        miningBiz.applyNewMiningPoint(BaseContextHandler.getUserID(),pointId,false,new Date());
        return AppResponse.ok();


    }
    @RequestMapping(value = "getSelfInfo")
    public AppResponse getSelfInfo(){

        return AppResponse.ok().data(miningBiz.getUserGroupInfo(BaseContextHandler.getUserID()));

    }

    @RequestMapping(value = "getSelfPoints")
    public AppResponse getSelfPoints(){
        return AppResponse.ok().data(miningBiz.getUserMinings(BaseContextHandler.getUserID()));

    }
    @RequestMapping(value = "obtain")
    public AppResponse obtain(String miningId){
        return AppResponse.ok().data(miningBiz.obtain(BaseContextHandler.getUserID(),miningId));

    }

    @RequestMapping(value = "getGroupLevelCondition")
    public AppResponse getGroupLevelCondition() {


        return AppResponse.ok().data(miningBiz.getPersonLevelCondition(BaseContextHandler.getUserID()));
    }
    @RequestMapping(value = "queryMiningLog")
    public AppResponse queryMiningLog(int page,int size) {
        return AppResponse.ok().data(miningBiz.queryLog(BaseContextHandler.getUserID(),page,size));

    }

    @RequestMapping(value = "self/initNets")
    @IgnoreUserToken

    public String initNet()
    {
        userInviterBiz.init();
        return "ok";
    }
    @RequestMapping(value = "self/addGroupNum")
    @IgnoreUserToken

    public String addGroupNum(String userId,int num)
    {
        var net= userInviterBiz.userNetMap.get(userId);
        net.addGroupNum(num);
        return "ok";
    }
    @RequestMapping(value = "self/addPowers")
    @IgnoreUserToken

    public String addPowers(BigDecimal num)
    {
        MiningBiz.miningPower.setCurrentPower(num);
        return "ok";
    }
    @RequestMapping(value = "self/getNets")
    @IgnoreUserToken
    public JSONObject getNets(String userId)
    {
        var net=userInviterBiz.userNetMap.get(userId);

        JSONObject  jsonObject=JSONObject.parseObject(JSONObject.toJSONString(net));
        return jsonObject;
    }

    @RequestMapping(value = "getAllUserNets")
    @IgnoreUserToken

    public  List<UserNet> getAllUserNets()
    {

        List<UserNet>userNets=new ArrayList<>();
        for(UserNet net:userInviterBiz.userNetMap.values()){
            userNets.add(net);
        }
        return userNets;




    }
    @RequestMapping(value = "obtainLevel")
    public AppResponse obtainLevel(String levelId)
    {
        String userId=BaseContextHandler.getUserID();

          boolean r= miningBiz.obtainLevel(userId,levelId);
          return AppResponse.ok().data(r);
    }

    @RequestMapping(value = "self/addGroupInfo")
    @IgnoreUserToken

    public String addGroupInfo(String userId,Integer groupNum,BigDecimal groupPower,BigDecimal directPower,Integer directNum,Integer directKyc)
    {
        var net= userInviterBiz.userNetMap.get(userId);
        if(groupNum!=null) net.addGroupNum(groupNum);
        if(groupPower!=null) net.addGroupPower(groupPower);
        if(directPower!=null) net.addDirectPower(directPower);
        if(directNum!=null) net.addDirectNum(directNum);
        if(directKyc!=null)net.addDirectKyc(directNum);
        return "ok";
    }
    @RequestMapping(value = "self/addUserPoint")
    @IgnoreUserToken

    public String addUserPoint(String userId,String pointId )
    {
       miningBiz.obtainNewPoint(userId,new Date(),miningBiz.getPoint(pointId));
        return "ok";
    }

    @RequestMapping(value = "self/prizeKyc")
    @IgnoreUserToken

    public String prizeKyc( )
    {
        miningBiz.prizeKycNoMining();
        return "ok";
    }






}
